/*
 * FileName: CloudCorsFilter.java
 * Author:   Arshle
 * Date:     2018年04月13日
 * Description: 云平台统一CORS拦截器
 */
package com.chezhibao.trace.filter;

import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 〈统一CORS拦截器〉<br>
 * 〈统一CORS拦截器〉
 *
 * @author Arshle
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本]（可选）
 */
@Component
public class CloudCorsFilter implements Filter {

    /**
     * @param servletRequest request
     * @param servletResponse response
     * @param filterChain 过滤链
     * @throws IOException IO异常
     * @throws ServletException Servlet异常
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig){}

    @Override
    public void destroy() {}
}
